package ProjectTeam39.GUITest;

import ProjectTeam39.Domains.*;


import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;

public class LoginFrame extends JFrame {
	PatientFrame pframe;
	NurseFrame nframe;
	DoctorFrame dframe;
	AdminFrame aframe;
	LoginFrame lframe = this;
	private JTextField txtUsername;
	private JPasswordField pwdPassword;
	private int attempts = 0;
	UserList ul;
	
	public LoginFrame(final UserList ul){
		this.ul = ul;
		JPanel panel = new JPanel();
		this.getContentPane().add(panel, BorderLayout.NORTH);
		
		JPanel panel_3 = new JPanel();
		panel.add(panel_3);
		
		txtUsername = new JTextField();
		panel_3.add(txtUsername);
		txtUsername.setText("Username");
		txtUsername.setColumns(10);
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2);
		pwdPassword = new JPasswordField();
		panel_2.add(pwdPassword);
		pwdPassword.setText("Password");
		pwdPassword.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.SOUTH);
		
		JSplitPane splitPane = new JSplitPane();
		panel_1.add(splitPane);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String username = txtUsername.getText();
				String pw = new String(pwdPassword.getPassword());
				int loggedIn = doLogin(username,pw);
				if(loggedIn == 0){
					attempts++;
				}
				else{
					switch(loggedIn){
					case 1: goToPatient(); 	break;
					case 2: goToNurse(); 	break;
					case 3: goToDoctor(); 	break;
					case 4: goToAdmin();	break;
					}//end switch
				}
				//end if
				if(attempts>2){
					goToError();
				}//end if
				//goToNurse();
			}//end ActionPerformed
		});
		splitPane.setLeftComponent(btnSubmit);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				lframe.dispose();
			}
		});
		splitPane.setRightComponent(btnCancel);
		}//end constructor
	public int doLogin(String username, String pw){
		LoginVerifier LV = new LoginVerifier(ul);
		int logged = LV.loginAttempt(username, pw);
		return logged;
	}
	
	public void goToPatient(){
		pframe = new PatientFrame(ul);
		lframe.setVisible(false);
		pframe.setVisible(true);
		pframe.setBounds(100, 100, 450, 300);
		pframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void goToNurse(){
		nframe = new NurseFrame(ul);
		this.setVisible(false);
		nframe.setBounds(100, 100, 600, 300);
		nframe.setVisible(true);
		nframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void goToDoctor(){
		dframe = new DoctorFrame(ul);
		this.setVisible(false);
		dframe.setBounds(100, 100, 450, 300);
		dframe.setVisible(true);
		dframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void goToAdmin(){
		aframe = new AdminFrame(ul);
		this.setVisible(false);
		aframe.setBounds(100, 100, 450, 300);
		aframe.setVisible(true);
		aframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void goToError(){
		ErrorFrame eframe = new ErrorFrame(this, "You have failed to login");
		this.setVisible(false);
		attempts = 0;
		eframe.setBounds(100, 100, 450, 300);
		eframe.setVisible(true);
		eframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public int getAttempts(){
		return attempts;
	}
}